package com.xge.component_caipu

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.blankj.utilcode.util.ToastUtils
import com.xge.common_libary.bean.CaiPuListBean
import com.xge.common_libary.bean.CaiPuListInfoBean
import com.xge.common_libary.bean.CaiPuListInfoSection
import com.xge.common_libary.bean.CaiPuListSction
import com.xge.common_libary.http.ApiResult
import com.xge.common_libary.utils.SingleDataHolder
import com.xge.component_caipu.binder.ItemCaiPuClassifyBinder
import com.xge.component_caipu.binder.ItemCaiPuContent
import com.xge.component_caipu.binder.ItemCaiPuTabsBinder
import com.xge.component_caipu.vm.CaiPuVM
import me.drakeet.multitype.MultiTypeAdapter
import om.xge.component_caipu.databinding.FragmentCaipuBinding

class CaiPuFragment : Fragment() {

    private lateinit var binding: FragmentCaipuBinding
    private val viewModel: CaiPuVM by viewModels()
    private var selectMenu: SingleDataHolder<String?>? = null
    private var selectTab: SingleDataHolder<String?>? = null
    private var page = 1
    private var classid = -1
    private var contentList = arrayListOf<CaiPuListInfoSection>()
    private lateinit var contentAdapter:MultiTypeAdapter
    private var isRefresh = false

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        binding = FragmentCaipuBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        selectMenu = SingleDataHolder()
        selectTab = SingleDataHolder()

        initContentData()

        viewModel.getCaiPuList()
        viewModel.getCaiPuList.observe(viewLifecycleOwner) { result ->
            when (result) {
                is ApiResult.Error -> ToastUtils.showShort(result.message)
                is ApiResult.Exception -> ToastUtils.showShort(result.exception)
                is ApiResult.Success -> initData(result.data)
            }
        }

        viewModel.getCaiPuListInfo.observe(viewLifecycleOwner) { result ->
            when (result) {
                is ApiResult.Error -> ToastUtils.showShort(result.message)
                is ApiResult.Exception -> ToastUtils.showShort(result.exception)
                is ApiResult.Success -> bindingContentData(result.data)
            }
        }

        binding.refreshLayout.setOnRefreshListener {
            page = 1
            viewModel.getCaiPuListInfo(classid, page)
            isRefresh = true

        }
        binding.refreshLayout.setOnLoadMoreListener {
            page++
            viewModel.getCaiPuListInfo(classid, page)
        }

        binding.ivSearch.setOnClickListener {
            SearchActivity.start(requireContext())
        }
    }

    private fun initContentData(){
        contentAdapter = MultiTypeAdapter(contentList)
        contentAdapter.register(CaiPuListInfoSection::class.java, object :ItemCaiPuContent(){
            override fun onItemClick(item: CaiPuListInfoSection) {
                CaiPuDetailActivity.start(requireContext(),item)
            }
        })
        binding.rvContent.adapter = contentAdapter
    }

    private fun bindingContentData(data: CaiPuListInfoBean?) {
        data?.list?.let {
            if(isRefresh){
                contentList.clear()
                binding.refreshLayout.finishRefresh()
                isRefresh = false
            }else{
                binding.refreshLayout.finishLoadMore()
            }
            contentList.addAll(it)
            contentAdapter.notifyDataSetChanged()

        }
    }

    private fun initData(data: List<CaiPuListBean>) {
        if (!data.isNullOrEmpty()) {

            selectMenu?.addOrUpdate(data[0].name)

            val adapter = MultiTypeAdapter(data)
            adapter.register(
                CaiPuListBean::class.java,
                object : ItemCaiPuClassifyBinder(selectMenu) {
                    override fun onSelectItem(item: List<CaiPuListSction>?) {
                        item?.let {
                            initTabData(it)
                        }
                    }
                })
            binding.rvMenu.adapter = adapter
        }
    }

    private fun initTabData(item: List<CaiPuListSction>) {
        selectTab?.addOrUpdate(item[0].name)
        val adapter = MultiTypeAdapter(item)
        adapter.register(CaiPuListSction::class.java, object : ItemCaiPuTabsBinder(selectTab) {
            override fun onSelectItem(item: CaiPuListSction) {
                isRefresh = true
                classid = item.classid!!
                viewModel.getCaiPuListInfo(item.classid, page)
            }
        })
        binding.rvTab.adapter = adapter
    }

    companion object {
        fun newInstance() = CaiPuFragment()
    }
}